﻿#-------------------------------------------------------------------------------
$OnLoadForm_StateCorrection=
{#Correct the initial state of the form to prevent the .Net maximized form issue
	$form1.WindowState = $InitialFormWindowState
	Initialize-Form
}
#-------------------------------------------------------------------------------
function Initialize-Form
{
}
#-------------------------------------------------------------------------------
$handler_listBox1_Click= 
{
}
#-------------------------------------------------------------------------------
$handler_listBox1_SelectedIndexChanged= 
{
	if ($ScriptData.CurrentListBoxItem -ne "")
	{
		## Updating data in GLCardList
		Write-Log $ScriptData.CurrentListBoxItem
		$GLCardKey = Get-GLCardKey -String $ScriptData.CurrentListBoxItem
		Write-Log $GLCardKey.Text
		$CheckedListBoxItems = Get-CheckedListBoxItems $checkedListBox1
		foreach ($Meaning in $GLCardList.($GLCardKey.ShortKey).Meaning.($GLCardKey.PartOfSpeech))
		{
			$Meaning.Checked = $CheckedListBoxItems[$Meaning.Value].Checked
		}
	}
	Remove-CheckedListBoxItems $checkedListBox1 -All
	$ScriptData.CurrentListBoxItem = Get-ListBoxSelectedItem $listBox1
	$GLCardKey = Get-GLCardKey -String $ScriptData.CurrentListBoxItem
	foreach ($Meaning in $GLCardList.($GLCardKey.ShortKey).Meaning.($GLCardKey.PartOfSpeech))
	{
		Add-CheckedListBoxItem $checkedListBox1 $Meaning.Value $Meaning.Checked
	}
}
#-------------------------------------------------------------------------------
$handler_btnBrowse_Click= 
{
	$openFileDialog1.Filter = "All Files (*.*)|*.*"
	$openFileDialog1.Title = "Select file"
	$Show = $openFileDialog1.ShowDialog()
	if ($Show -eq "OK")
	{
		$ScriptData.InputFilePath = $openFileDialog1.FileName
		Write-Log $ScriptData.InputFilePath
	}
	else
	{
		$ScriptData.InputFilePath = ""
		Write-Log "No file selected!"
	}
}
#-------------------------------------------------------------------------------
$handler_btnTranslate_Click= 
{
	$ScriptData.MaxTextLength = 0
	if ($ScriptData.InputFilePath -ne "")
	{
		Get-Content -Path $ScriptData.InputFilePath |
			Get-WordFromDictionary | 
			ForEach-Object {
				$GlCardKey = Get-GLCardKey -WordCard $_
				#$ScriptData.MaxTextLength = [Math]::Max($ScriptData.MaxTextLength, $GLCardKey.Text.Length)
				$GLCardList.Add($GLCardKey.ShortKey, $_) | Out-Null
				foreach ($pos in $_.Meaning.Keys)
				{
					$GLCardKey = Get-GLCardKey -WordCard $_ -PartOfSpeech $pos
					Add-ListBoxItem $listBox1 -ItemList $GLCardKey.LongKey
				}
			}
	}
}
#-------------------------------------------------------------------------------
$handler_btnExport_Click= 
{
#TODO: Place custom script here
	Export-GLCardsToGLFile
}
#-------------------------------------------------------------------------------
$handler_btnSetAll_Click= 
{
#TODO: Place custom script here
	Set-CheckedListBoxItemState $checkedListBox1 -Set
}
#-------------------------------------------------------------------------------
$handler_btnClearAll_Click= 
{
#TODO: Place custom script here
	Set-CheckedListBoxItemState $checkedListBox1 -Clear
}
#-------------------------------------------------------------------------------
$handler_btnInvert_Click= 
{
#TODO: Place custom script here
	Set-CheckedListBoxItemState $checkedListBox1 -Invert
}
#-------------------------------------------------------------------------------


##------------------------------------------------------------------------------
function Add-ListBoxItem
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.ListBox"})]
		$InputObject,
		[String[]] $ItemList
	)
	$InputObject.BeginUpdate()
	foreach ($Item in $ItemList)
	{
		$InputObject.Items.Add($Item)
	}
	$InputObject.EndUpdate()
}
##------------------------------------------------------------------------------
function Get-ListBoxSelectedItem
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.ListBox"})]
		$InputObject
	)
	$InputObject.SelectedItem.ToString()
}
##------------------------------------------------------------------------------
function Add-CheckedListBoxItem
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.CheckedListBox"})]
		$InputObject,
		[String[]] $ItemList,
		[Boolean] $Checked
	)
	foreach($Item in $ItemList)
	{
		$InputObject.Items.Add($Item, $Checked)
	}
}
##------------------------------------------------------------------------------
function Remove-CheckedListBoxItems
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.CheckedListBox"})]
		$InputObject,
		[Switch] $All
	)
	if ($All)
	{
		$InputObject.Items.Clear()
		return
	}
}
##------------------------------------------------------------------------------
function Get-CheckedListBoxItems
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.CheckedListBox"})]
		$InputObject
	)
	$ItemList = @{}
	foreach ($Item in $InputObject.Items)
	{
		$ItemText = $Item.ToString()
		$ItemIndex = $InputObject.Items.IndexOf($Item)
		$ItemChecked = $InputObject.GetItemChecked($ItemIndex)
		$ItemList.Add($ItemText, @{
			"Index"   = $ItemIndex
			"Text"    = $ItemText
			"Checked" = $ItemChecked}
			)
	}
	$ItemList
}
##------------------------------------------------------------------------------
function Set-CheckedListBoxItemState
{
	param
	(
		[Parameter(ValueFromPipeline=$true)]
		[ValidateScript({$_.GetType().ToString() -eq "System.Windows.Forms.CheckedListBox"})]
		$InputObject,
		[Switch] $Set,
		[Switch] $Clear,
		[Switch] $Invert
	)
	$ItemList = @()
	foreach ($Item in $InputObject.Items)
	{
		$ItemList += @(@{
			"Index" = $InputObject.Items.IndexOf($Item)
		})
	}
	foreach ($Item in $ItemList)
	{
		if ($Set)
		{
			$InputObject.SetItemChecked($Item.Index, $true)
		}
		if ($Invert)
		{
			$ItemChecked = $InputObject.GetItemChecked($Item.Index)
			$InputObject.SetItemChecked($Item.Index, !$ItemChecked)
		}
		if ($Clear)
		{
			$InputObject.SetItemChecked($Item.Index, $false)
		}
	}
}
##------------------------------------------------------------------------------
